--[[
* @author PureMVC LUA Native Port by Ravior 
* @author Copyright(c) 2015 Gitlib, Inc., Some rights reserved.
* 
* @class puremvc.Notifier
* 
* A Base Notifier implementation.
* 
* {@link puremvc.MacroCommand MacroCommand}, 
* {@link puremvc.SimpleCommand SimpleCommand}, 
* {@link puremvc.Mediator Mediator} and 
* {@link puremvc.Proxy Proxy}
* all have a need to send Notifications
* 
* The Notifier interface provides a common method called #sendNotification that 
* relieves implementation code of the necessity to actually construct 
* Notifications.
* 
* The Notifier class, which all of the above mentioned classes
* extend, provides an initialized reference to the 
* {@link puremvc.Facade Facade}
* Multiton, which is required for the convienience method
* for sending Notifications but also eases implementation as these
* classes have frequent 
* {@link puremvc.Facade Facade} interactions 
* and usually require access to the facade anyway.
* 
* NOTE: In the MultiCore version of the framework, there is one caveat to
* notifiers, they cannot send notifications or reach the facade until they
* have a valid multitonKey. 
* 
* The multitonKey is set:
*   - on a Command when it is executed by the Controller
*   - on a Mediator is registered with the View
*   - on a Proxy is registered with the Model. 
]]

local Notifier = class("Notifier")


--[[
* @constructor 
]]
function Notifier:ctor() end


--[[
* Create and send a Notification.
*
* Keeps us from having to construct new Notification instances in our 
* implementation code.
* 
* @param {string} notificationName
*  A notification name
* @param {Object} [body]
*  The body of the notification
* @param {string} [type]
*  The notification type
* @return {void}
]]
function Notifier:sendNotification(notificationName,body,type)
	local facade = self:getFacade()
	if facade ~= nil then
		facade:sendNotification(notificationName,body,type)
	end
end


--[[
* Initialize this Notifier instance.
* 
* This is how a Notifier gets its multitonKey. 
* Calls to #sendNotification or to access the
* facade will fail until after this method 
* has been called.
* 
* Mediators, Commands or Proxies may override 
* this method in order to send notifications
* or access the Multiton Facade instance as
* soon as possible. They CANNOT access the facade
* in their constructors, since this method will not
* yet have been called.
* 
*
* @param {string} key
*  The Notifiers multiton key;
* @return {void}
]]
function Notifier:initializeNotifier(key)
	self.multitonKey = tostring(key)
	self.facade = self:getFacade()
end


--[[
* Retrieve the Multiton Facade instance
*
*
* @protected
* @return {puremvc.Facade}
]]
function Notifier:getFacade()
	if self.multitonKey == nil then
		error(Notifier.MULTITON_MSG)
	end
	return Facade:getInstance(this.multitonKey);
end


--[[
* @ignore
* The error message used if the Notifier is not initialized correctly and
* attempts to retrieve its own multiton key
*
* @static
* @protected
* @const
* @type string
]]
Notifier.MULTITON_MSG = "multitonKey for this Notifier not yet initialized!";

return Notifier